package com.asp.alumnisystem.ui;

import android.view.View;
import android.widget.EditText;

import com.asp.alumnisystem.R;
import com.asp.alumnisystem.app.App;
import com.asp.alumnisystem.base.BaseActivity;
import com.asp.alumnisystem.base.BaseBean;
import com.asp.alumnisystem.base.BasePresenter;
import com.asp.alumnisystem.base.IView;
import com.asp.alumnisystem.model.UserModel;
import com.asp.alumnisystem.net.Config;
import com.asp.alumnisystem.presenter.LoginPresenter;
import com.asp.alumnisystem.utils.DialogUtils;
import com.asp.alumnisystem.utils.StringUtils;
import com.asp.alumnisystem.utils.ToastUtil;
import com.asp.alumnisystem.utils.UiUtils;
import com.asp.alumnisystem.view.MyEditText;
import com.tencent.mmkv.MMKV;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity implements IView<UserModel> {

    @BindView(R.id.user_view)
    MyEditText userView;
    @BindView(R.id.pass_view)
    MyEditText passView;
    @BindView(R.id.ip_view)
    MyEditText ipView;

    private LoginPresenter loginPresenter;

    private boolean isSecondBackPressed;
    private long firstTime;

    @Override
    protected int getLayoutId() {
        return R.layout.login_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        loginPresenter = new LoginPresenter(this);
        managerPresenter(loginPresenter);
        userView.setText(MMKV.defaultMMKV().decodeString("loginUser"));
        ipView.setText(MMKV.defaultMMKV().decodeString("loginIp", Config.DEFAULT_IP));
    }

    @Override
    public void onBack() {
        if (isSecondBackPressed) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime < 2000) {
                this.finish();
            } else {
                firstTime = System.currentTimeMillis();
                ToastUtil.showShortToast(getString(R.string.twice_click_exit));
            }
        } else {
            isSecondBackPressed = true;
            firstTime = System.currentTimeMillis();
            ToastUtil.showShortToast(getString(R.string.twice_click_exit));
        }
    }

    @OnClick({R.id.login_view, R.id.visitor_login_view})
    public void click(View view){
        switch (view.getId()){
            case R.id.login_view:
                String hostIp = ipView.getText().toString();
                if (StringUtils.isNull(hostIp)){
                    ToastUtil.showLongToast("主机IP不能为空");
                }else{
                    MMKV.defaultMMKV().encode("loginIp", hostIp);
                    loginPresenter.setLoginInfo(userView.getText().toString(), passView.getText().toString(), hostIp);
                    loginPresenter.work(false);
                }
                break;
            case R.id.visitor_login_view:
                String hostIp2 = ipView.getText().toString();
                if (StringUtils.isNull(hostIp2)){
                    ToastUtil.showLongToast("主机IP不能为空");
                    MMKV.defaultMMKV().encode("loginIp", hostIp2);
                }else {
                    loginPresenter.setLoginInfo(userView.getText().toString(), passView.getText().toString(), ipView.getText().toString());
                    loginPresenter.work(true);
                }
                break;
        }
    }

    @Override
    public void loadSuccess(BasePresenter presenter, BaseBean<UserModel> result) {
        if (result.success){
            App.getApp().setUserModel(result.data);
            UiUtils.jumpToPageAndFinishSelf(this, HomeActivity.class);
        }else{
            ToastUtil.showShortToast(result.error);
        }
    }

    @Override
    public void loadStart(BasePresenter presenter) {
        super.loadStart(presenter);
        DialogUtils.showLoadDialog(this, "登录中...");
    }

    @Override
    public void loadFail(BasePresenter presenter, String error) {
        super.loadFail(presenter, error);
        ToastUtil.showShortToast(error);
    }

    @Override
    public void loadDone(BasePresenter presenter) {
        super.loadDone(presenter);
        DialogUtils.dismiss();
    }
}
